package org.uugu.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import org.uugu.dao.WishesDAO;
import org.uugu.model.Wishes;
import org.uugu.service.AllService;
import org.uugu.service.WishesManager;
import org.uugu.util.PageControl;


@Component("wishesManager")
public class WishesManagerImpl extends AllService implements WishesManager {

	private WishesDAO wishesDao;
	private Log log = LogFactory.getLog(WishesManagerImpl.class);
 

//================================Add Wishes==================================//
	@Override
	public String add(Wishes wish) {
		try{
			//log.info("============="+wish.getWishOwner()+wish.getWishOwnerstate());
			wishesDao.save(wish);
			
			return "addSuccess";
		}catch(Exception e){
			e.printStackTrace();
			return "addFalse";
		}			
	}
	
//==============================Del Wishes==================================//
	@Override
	public String delById(Integer wishid) {
		try {
			wishesDao.delete(wishesDao.findById(wishid));
			return "delSuccess";
		} catch (Exception e) {
			return "delFalse";
		}
	}

//==============================列出所有通过的祝福===================================//
	@Override
	public List<Wishes> listAllPassWishes() {
		try {
			//List wishes=wishesDao.findByWishState(1);
			List<Wishes> wishes=wishesDao.getHibernateTemplate().find("from Wishes as w where w.wishState=1 order by w.wishId desc");
			return wishes;
		} catch (Exception e) {
			return null;
		}
	}

//==============================列出指定数目的通过的祝福===================================//
	@Override
	public List<Wishes> listPassWishesByNum(Integer num) {
		try {
			if(num<=wishesDao.findByWishState(1).size()){
				List<Wishes> wishes=wishesDao.getHibernateTemplate().find("from Wishes as w where w.wishState=1 order by w.wishId desc").subList(0, num);
				return wishes;
			}else{
				List<Wishes> wishes=wishesDao.getHibernateTemplate().find("from Wishes as w where w.wishState=1 order by w.wishId desc");
				return wishes;
			}			
		} catch (Exception e) {
			return null;
		}
	}
	
	/**************************** 前台调用的方法，按页数显示数据***************************/
	@Override
	public List listWishesInPage(PageControl pc) {
		try {
			int total=wishesDao.findByWishState(1).size();
			pc.execute(total, pc.getPageSize(), pc.getPage());	
			if(total>pc.getPageSize()){
				List wishes = wishesDao.getHibernateTemplate().find("from Wishes as w where w.wishState=1 order by w.wishId desc").subList(pc.getSub1(), pc.getSub2());
				return wishes;
			}else{
				List wishes = wishesDao.getHibernateTemplate().find("from Wishes as w where w.wishState=1 order by w.wishId desc");
				return wishes;
			}
				
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**************************** 前台调用的方法，按页数显示数据***************************/
	@Override
	public List listUnpassWishesInPageBack(PageControl pc) {
		try {
			int total=wishesDao.findByWishState(0).size();
			pc.execute(total, pc.getPageSize(), pc.getPage());	
			if(total>pc.getPageSize()){
				List wishes = wishesDao.getHibernateTemplate().find("from Wishes as w where w.wishState=0 order by w.wishId desc").subList(pc.getSub1(), pc.getSub2());
				return wishes;
			}else{
				List wishes = wishesDao.getHibernateTemplate().find("from Wishes as w where w.wishState=0 order by w.wishId desc");
				return wishes;
			}
				
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
//==============================列出所有未通过的祝福===================================//
	@Override
	public List listAllUnpassWishes() {
		try {
			List wishes=wishesDao.findByWishState(0);
			return wishes;
		} catch (Exception e) {
			return null;
		}
	}
	
//=================================列出所有的祝福=====================================//	
	@Override
	public List listAll() {
		// TODO Auto-generated method stub
		return null;
	}

//================================通过ID审核通过祝福留言==============================//
	@Override
	public String passWishById(Integer wishid) {
		// TODO Auto-generated method stub
		try {
			Wishes wish = new Wishes();
			wish = wishesDao.findById(wishid);
			wish.setWishState(1);
			wishesDao.merge(wish);
			return "success";
		} catch (Exception e) {
			return "false";
		}
	}
	

	
	
//===============================Getters And Setters==========================//
	public WishesDAO getWishesDao() {
		return wishesDao;
	}
	
	@Resource
	public void setWishesDao(WishesDAO wishesDao) {
		this.wishesDao = wishesDao;
	}

}
